{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}用户登录{% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/register_login.css' %}">
{% endblock %}
{% block content %}
    <div class="reg">
        <h1 class="title">短信登录</h1>
        <form id="regForm" method="POST">
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <button type="button" class="btn btn-primary">登 录</button>
        <a href="{% url 'app:login' %}" style="display: inline-block;text-decoration: none;float: right">用户名密码登录</a>
        </form>
    </div>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            $('.error-msg').empty();
            bindClickBtnSms();
        })

        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                // 收集表单中的数据（找到每一个字段）$('#regForm').serialize()
                // 数据ajax发送到后台
                $.ajax({
                    url: "{% url 'app:login_sms' %}",
                    type: "POST",
                    data: $('#regForm').serialize(), // 所有字段数据 + csrf token
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty();
                let phone = $('#id_phone').val();
                $.ajax({
                    url: "/api/v1/send/sms/",
                    type: 'POST',
                    data: {'phone': phone, 'tpl': 'login'},
                    dataType: 'JSON',
                    success: function (res) {
                        if (res['status']) {
                            sendSmsRemind()
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true); // 禁用
            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)

        }
    </script>
{% endblock %}